package com.qst.service;

import com.qst.dao.DepartmentDao;
import com.qst.dao.IEmployeeDao;
import com.qst.pojo.Department;
import com.qst.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Eumenides.
 * @create 2021-06-20 15:37
 */
@Service
public class RegisterService {
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private IEmployeeDao employeeDao;

    /**
     * 获得所有的部门信息
     *
     * @return java.util.List<com.qst.pojo.Department>
     * @Param []
     * @author Eumenides
     */
    public List<Department> getAllDepartments() {
        return departmentDao.findDep();
    }

    /**
     * 插入一个新员工
     *
     * @return int
     * @Param [employee]
     * @author Eumenides
     */
    public String insertEmployee(Employee employee, Integer did) {
        employee.setStatus(0);
        employee.setRole("1");
        employee.setDepartment(new Department(did, null));
        int success = employeeDao.insertEmployee(employee);
        return success == 1 ? "success" : "failed";
    }

    public Integer repetition(Employee employee) {
        int status = 0;

        if(employee.getUsername() != null && employeeDao.judgeUsername(employee.getUsername()) != null) {
            status |= 1;
        }
        if(employee.getPhone() != null && employeeDao.judgePhone(employee.getPhone()) != null) {
            status |= 2;
        }
        if(employee.getEmail() != null && employeeDao.judgeEmail(employee.getEmail()) != null) {
            status |= 4;
        }

        return status;
    }
}
